package com.tianpengtech.front.mobile.message;

import com.jfinal.plugin.activerecord.Page;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairmessage.FairMessage;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.web.fair.FairService;

import java.util.List;

/**
 * Created by ggd on 2017/7/23 0023.
 */
public class MobileMessageService {
    public static MobileMessageService me = new MobileMessageService();
    private static FairMessage fairMessage = new FairMessage();
    private static FairService fs = new FairService();
    /**
     * 发送消息
     * @param uid
     * @param content
     */
    public R sendMessage(long uid,String content){
        FairMessage fm = new FairMessage();
        fm.set(FairMessage.WH_ID,uid);
        fm.set(FairMessage.CONTENT,content);
        fm.save();
        return R.ok("发送成功");
    }


    /**
     * 获取消息列表
     * @param uid
     */
    public R messageList(int pageNum, int length, long uid){
        String sql = String.format(" from %s where %s=? and %s=? order by %s desc,%s asc",fairMessage.tableName(),FairMessage.DELETETAG,FairMessage.WH_ID,FairMessage.CREATE_TIME,FairMessage.ISREAD);
        Page<FairMessage> page=fairMessage.paginate(pageNum,length,"select *",sql,0,uid);
        R r = R.ok("成功");
        r.p("count",page.getTotalRow());
        r.p("pageNum",page.getPageNumber());
        r.p("isLastPage",page.isLastPage());
        List<FairMessage> list = page.getList();
        list.forEach( f ->{
            f.remove(FairMessage.WH_ID);
            f.remove(FairMessage.DELETETAG);
            String content=f.get(FairMessage.CONTENT);
            f.remove(FairMessage.CONTENT);
            f.put("content",handleContent(content));
        });
        r.p("list",list);
        return r;
    }

    private String handleContent(String content){
        int len = 150;
        if (content.length()>len){
            content=content.substring(0,len);
            content+="...";
            return content;
        }else{
            return content;
        }
    }

    /**
     * 获取消息详情
     * @return
     */
    public R getMessage(String id,long uid){
        String sql = String.format("select * from %s where %s=? and %s=? and %s=?",fairMessage.tableName(),FairMessage.DELETETAG,FairMessage.WH_ID,FairMessage.ID);
        FairMessage fm=fairMessage.findFirst(sql,0,uid,id);
        if (fm != null){
            fm.remove(FairMessage.WH_ID);
            fm.remove(FairMessage.DELETETAG);
            fm.remove(FairMessage.ISREAD);
            R r = R.ok("成功");
            r.p("message",fm);
            fm.set(FairMessage.ISREAD,FairMessage.ISREAD_READ);
            fm.update();
            return r;
        }else{
            return R.fail("消息不存在");
        }
    }

    /**
     * 某个用户是否有未读消息
     * @param uid
     * @return
     */
    public R isUnreadMsg(long uid){
        R r = R.ok("成功");
        Fair fair=fs.findById(Integer.parseInt(uid+""));
        int remind=1;
        if (fair!=null){
            remind=fair.getInt(Fair.REMIND_MSG);
        }
        if (remind==Fair.REMIND_MSG_OFF){
            r.p("isread","false");
        }else{
            String sql = String.format("select * from %s where %s = ? and %s = ? and %s = ?",fairMessage.tableName(),FairMessage.DELETETAG,FairMessage.ISREAD,FairMessage.WH_ID);
            FairMessage fm=fairMessage.findFirst(sql,1,FairMessage.ISREAD_UNREAD,uid);
            if (fm==null){
                r.p("isread","false");
            }else{
                r.p("isread","true");
            }
        }
        return r;
    }

}
